#include "sort.h"

void sort_str(char *target[],int num){
     char *temp;
     int top,seek;

     for (top  = 0;  top< num-1; top++)
     {
         for (seek  = top+1; seek < num; seek++)
         {
          if (strcmp(target[top],target[seek])>0)

          {
               temp = target[top];
               target[top] = target[seek];
               target[seek] = temp;

          }
          
         }
         
     }
     
}

void test_sort(){

     char input[LIMT][SIZE];
     char *ptstr[LIMT];

     int input_count =0;
     int output_count;

     printf("Input up to %d lines,and I will sort them.\n",LIMT);
     printf("to stop,press the Enter key at a line's start.\n");

     while (input_count <LIMT  && s_gets(input[input_count],SIZE) !=NULL && input[input_count][0]!='\0')
     {
          ptstr[input_count] = input[input_count];//设置指针指向字符串
          input_count ++;
     }
     

     sort_str(ptstr,input_count);

     printf("Here are the sorted strings:\n");
     for (output_count  = 0; output_count < input_count; output_count++)
     {
          puts(ptstr[output_count]);

     }
     

}

char * s_gets(char *st,int n){
     char * ret_val;
     int i=0;

     ret_val = fgets(st,n,stdin);
     if (ret_val)
     {
          while (st[i] != '\n' && st[i] != '\0')
          {
               i++;
          }
          if (st[i] == '\n')
          {
              st[i] = '\0';
          }

          else {
               while (getchar() != '\n')
               {
                    continue;
               }
               
          }
          
          
     }

     return ret_val;
     
}